package com.xidian.zhiancloud.controller.equipFacility.preUnit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.PuIn;
import com.xidian.zhiancloud.model.Warehousing;
import com.xidian.zhiancloud.qo.equipFacility.PreUnitQO;
import com.xidian.zhiancloud.qo.equipFacility.PuInQO;
import com.xidian.zhiancloud.service.equipFacility.PuInService;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehousingVO;
import com.xidian.zhiancloud.vo.equipFacility.PuInVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 备品备件入库
 */
@RestController
@RequestMapping("/PuIn")
public class PuInController extends BaseController {

    @Autowired
    private PuInService puInService;

    public int count = 0;
    public Date date = new Date();

    /**
     * 增加入库记录
     *
     * @param puIn 入库对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(PuIn puIn) {
        puIn.setRuTime(new Date(System.currentTimeMillis()));
        puIn.setRuer(getSessionUserid());
        puIn.setCompanyId(getSessionUserCompanyId());
        Date temp = new Date();
        if (count == 0) {
            date = temp;
        }
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(temp);
        cal2.setTime(date);
        if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
                cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)) {
            count++;
        } else {
            count = 0;
        }
        String str = cal1.get(Calendar.YEAR) + String.format("%02d", cal1.get(Calendar.MONTH) + 1) +
                String.format("%02d", cal1.get(Calendar.DAY_OF_MONTH)) + String.format("%03d", count);
        puIn.setCode(str);
        Boolean res = puInService.add(puIn);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取入库记录
     *
     * @param puInQO 入库对象
     * @return 入库对象
     */
    @GetMapping("/getPuIn")
    public ResultDTO<IPage<PuInVO>> getPuIn(PuInQO puInQO) {
        if (puInQO.getCurrent() == null || puInQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        puInQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(puInService.getPuIn(puInQO), GeneralResponseEnums.SUCCESS);
    }
}
